package org.example.springbirthday.interceptor;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.example.springbirthday.constants.Constants;
import org.example.springbirthday.utils.JwtUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/4
 * Time: 13:24
 */
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        //1.获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        log.info("接收到token:{}",token);
        //2.校验token
        Claims claims = JwtUtils.parseToken(token);
        log.info("claims:{}",claims);
        if(claims == null){
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
